<template>
  <div class="login_container">
    <div class="login_box">
      <div class="avatar_box">
        <img :src="require('@/assets/img/dltx.png')" alt="">
      </div>
      <el-form ref="loginFormRef" :model="loginForm" :rules="loginFormRules" class="login_form" label-position="right" label-width="100px">
        <el-text tag="b" style="margin-left: 105px;font-size: 18px;margin-bottom: 20px" >5A级球馆预约平台-登录</el-text>
        <el-form-item prop="account" label="账号" >
          <el-input v-model="loginForm.account" prefix-icon="el-icon-user-solid" placeholder="请使用账号/手机号/邮箱登录"></el-input>
        </el-form-item>
        <el-form-item prop="password" label="密码">
         <el-input v-model="loginForm.password" prefix-icon="el-icon-lock" show-password placeholder="请输入密码"></el-input>
        </el-form-item>
        <el-form-item class="btn">
          <el-button type="primary" @click="login">登录</el-button>
          <el-button type="info" @click="resetLoginForm">重置</el-button>
          <el-text class="mx-1" type="primary" tag="ins" style="cursor: pointer" @click="toRegister" > 没有账号？点击注册！</el-text>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import axios from 'axios';

export default {
  data() {
    return {
      loginForm: {
        account: '',
        password: ''
      },

      loginFormRules: {
        account: [{ required: true, message: '请输入账号', trigger: 'blur' }],
        password: [{ required: true, message: '请输入密码', trigger: 'blur' }]
      }
    }
  },
  methods: {
    login() {
      this.$refs.loginFormRef.validate((valid) => {
        if (valid) {
          // 表单验证通过，发送登录请求
          axios.post('http://39.101.75.128:8080/user/login', {
            account: this.loginForm.account,
            password: this.loginForm.password
          })
              .then(response => {
                console.log(response.data)
                if (response.data.code == 200) {
                  // 登录成功，跳转到/home
                  console.log(response.data)
                  console.log(response.data.data.isMember)

                  localStorage.setItem('userAccount', this.loginForm.account);
                  localStorage.setItem('isMember', response.data.data.isMember);
                  localStorage.setItem('phoneNumber', response.data.data.phoneNumber);
                  localStorage.setItem('userId', response.data.data.userId);
                  this.$router.push({path: '/home'});
                } else {
                  // 登录失败，提示错误信息
                  console.log("失败")
                  this.$message.error(response.data.msg);
                }
              })
              .catch(() => {
                // 请求发生错误，提示错误信息
                this.$message.error('登录失败，请稍后重试');
              });
        }
      });
    },
    resetLoginForm() {
      this.$refs.loginFormRef.resetFields();
    },
    toRegister(){
      this.$router.push({path: '/register'});
    }
  }
}
</script>

<style scoped>
	.login_container {
		box-sizing: unset !important;
		height: 100%;
		/*background-color: #2b4b6b;*/
    background: url('@/assets/img/bg_ljj.jpg');
	}

	.login_box {
		width: 450px;
		height: 300px;
		background-color: #fff;
		border-radius: 3px;
		position: absolute;
		left: 50%;
		top: 50%;
		transform: translate(-50%, -50%);
	}

	.login_box .avatar_box {
		height: 130px;
		width: 130px;
		border: 1px solid #eee;
		border-radius: 50%;
		padding: 10px;
		box-shadow: 0 0 10px #ddd;
		position: absolute;
		left: 50%;
		transform: translate(-50%, -50%);
		background-color: #fff;
	}

	.login_box .avatar_box img {
		width: 100%;
		height: 100%;
		border-radius: 50%;
		background-color: #eee;
	}

	.login_form {
		position: absolute;
		bottom: 0;
		width: 100%;
		padding: 0 20px;
		box-sizing: border-box;
	}
  .el-input{
    width: 80%;
    height: 30px;
    border: 1px #475669;
  }

	.btn {
    margin-left: 30px;
		display: flex;
		justify-content: flex-end;
	}
</style>
